package com.suoluo.zenadmin.service.crawler.resolver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import com.suoluo.zenadmin.service.crawler.bean.HotelReviewRecordBean;



class BookingCrawlerResolver extends AbstractCrawlerResolver{

	public BookingCrawlerResolver(CrawlerResovlerTask task) {
		super(task);
	}

	@Override
	protected int resolveReviewCount(Document doc) {
		String reviewCount = doc.select("p#rev_out_of").select("strong").first().ownText();
		reviewCount = reviewCount.substring(0,reviewCount.indexOf(" "));
//		if(!StringUtils.hasLength(reviewCount)){
//			reviewCount = doc.select("div.reviewFloater").select("span.score_from_number_of_reviews").select("strong.count").text().trim();
//		}
		return Integer.valueOf(reviewCount);
	}

	@Override
	protected double resolveReviewScore(Document doc) {
		String reviewScore = doc.select("strong#rsc_total").text();
//		if (!StringUtils.hasLength(reviewScore)){
//			reviewScore = doc.select("div.reviewFloater").select("span.rating").select("span.average").text().trim();
//		}
		return Double.valueOf(reviewScore);
	}

	@Override
	protected double resolveReviewFullScore(Document doc) {
		//默认10分制
		return 10.0;
	}

	@Override
	protected String resolveReviewLevel(Document doc) {
		Element reviewLevelElement = doc.select("div.hotel_large_photp_score").select("a.big_review_score_detailed").first();
		String reviewLevel = null;
		if (reviewLevelElement != null){
			reviewLevel = reviewLevelElement.ownText();
		}
		//http://www.booking.com/hotel/vn/the-nam-hai-hoi-an.zh-cn.html?aid=311096;label=hotel-71158-vn-j2B_b43HaEyewahhaukxjQS6256153457%3Apl%3Ata%3Ap1%3Ap2%3Aac%3Aap1t1%3Aneg;sid=1efddd9c20aa32d8294bf39f0350a9a7;dcid=1;srfid=360bc832f91a4be1d2167a5aab94d7a5a8e42d8bX1
		if (!StringUtils.hasLength(reviewLevel)){
			reviewLevel = doc.select("div.reviewFloater").select("a").first().ownText();
		}
		
		return reviewLevel;
	}

	@Override
	protected int resolveReviewRank(Document doc) {
		//默认没有排名
		return -1;
	}

	@Override
	protected List<HotelReviewRecordBean> resolveReviewRecords(Document doc) {
		List<HotelReviewRecordBean> records = new ArrayList<HotelReviewRecordBean>();
		Elements eles = doc.select("li.review_tr");
		Iterator<Element> iter = eles.iterator();
		int temp = 0;
		while (iter.hasNext()) {
			if (temp == 5) {
				break;
			}
			Element ele = iter.next();
			
			//评论描述
			String desc = ele.select("p.comments_good").text();
//			try {
//				System.out.println(StringUtil.filterOffUtf8Mb4(desc));
//			} catch (UnsupportedEncodingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			//没有评论直接过滤掉，继续抓取下一条
			if ( !StringUtils.hasLength(desc)){
				continue;
			}
			HotelReviewRecordBean record = new HotelReviewRecordBean();
			//没有标题
			record.setRecordTitle("");
			
			record.setRecordDesc(desc);
			
			//评论作者
			String author = ele.select("div.review_user_type").select("div.cell_user_name").text();
			if (!StringUtils.hasLength(author)){
				author = ele.select("div.cell_user").select("div.name").text();
			}
			record.setRecordAutor(author);
			
			String time = ele.select("div.review_user_type").select("span.cell_user_date").text();
			if (!StringUtils.hasLength(time)){
				time = ele.select("div.cell_user").select("div.date").text();
			}
			record.setRecordTime(time);
			
			//评论分数
			String score = ele.select("span.the_score").text().trim();
			record.setRecordScore(Double.valueOf(score));
			
			//评论总分数
			record.setRecordFullScore(10.0);
			temp++;
			records.add(record);
		}
		return records;
	}
	
//	public static void main(String []args){
//		String url = "http://www.booking.com/hotel/vn/the-nam-hai-hoi-an.zh-cn.html?aid=311096;label=hotel-71158-vn-j2B_b43HaEyewahhaukxjQS6256153457%3Apl%3Ata%3Ap1%3Ap2%3Aac%3Aap1t1%3Aneg;sid=1efddd9c20aa32d8294bf39f0350a9a7;dcid=1;srfid=360bc832f91a4be1d2167a5aab94d7a5a8e42d8bX1";
//		CrawlerResovlerTask task = new CrawlerResovlerTask(null);
//		task.setReviewUrl(url);
//		BookingCrawlerResolver r = new BookingCrawlerResolver(task);
//		r.doResolver();
//	}
} 
